<?php


namespace App\Http\Controllers\System;


use App\Http\Controllers\Controller;
use App\Http\Requests\System\Express\StoreRequest;
use App\Http\Requests\System\Express\UpdateRequest;
use App\Http\Searchs\System\ExpressSearch;
use App\Repositories\Express;

class ExpressController extends Controller
{
    /**
     * @var Express
     * @author luffyzhao@vip.126.com
     */
    private $express;

    /**
     * ExpressController constructor.
     * @param Express $express
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Express $express)
    {
        $this->express = $express;
    }

    /**
     * @param ExpressSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(ExpressSearch $search)
    {
        return $this->response(
            $this->express->paginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            $this->express->create($request->only([
                'code', 'name', 'customs_code', 'customs_name', 'phone', 'contacts', 'remark', 'insured'
            ]))
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id)
    {
        return $this->response(
            $this->express->find($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            $this->express->update($id, $request->only([
                'name', 'customs_code', 'customs_name', 'phone', 'contacts', 'remark', 'insured'
            ]))
        );
    }

    /**
     * @param $id
     * @param $status
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function status($id, $status){
        return $this->response(
            $this->express->status($id, $status)
        );
    }

}
